package com.kotlin.hxh.adapter

import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.kotlin.hxh.R
import com.kotlin.hxh.entity.Article
import com.kotlin.hxh.listener.OnItemClickListener

/**
 * @author huxh
 * @date 2018/12/18.
 */
class FirstHomeAdapter : androidx.recyclerview.widget.RecyclerView.Adapter<RecyclerViewHolder>() {
    private val mItems = arrayListOf<Article>()
    private var mClickListener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = RecyclerViewHolder.createViewHolder(parent.context, parent, R.layout.item_zhihu_home_first)

    override fun getItemCount() = mItems.size

    override fun onBindViewHolder(holder: RecyclerViewHolder, p1: Int) {
        val item = getItem(p1)
        holder.itemView.setOnClickListener {
            mClickListener?.onItemClick(holder.adapterPosition, it, holder)
        }
        ViewCompat.setTransitionName(holder.getView(R.id.img), p1.toString() + "_image")
        ViewCompat.setTransitionName(holder.getView(R.id.tv_title), p1.toString() + "_tv")
        holder.getView(R.id.img, ImageView::class.java).setImageResource(item.imgRes)
        holder.getView<TextView>(R.id.tv_title).text = item.title
    }

    fun setDatas(items: List<Article>) {
        mItems.clear()
        mItems.addAll(items)
    }

    fun setOnItemClickListener(itemClickListener: OnItemClickListener) {
        this.mClickListener = itemClickListener
    }

    fun getItem(position: Int) = mItems[position]

}

